/**
 * Created by chengming on 18/5/27.
 * version 1.0
 */
const ArticleHrefAdapter = require('../ArticleHrefAdapter');
const cheerio = require('cheerio');
const urlTool = require('url');

/**
 * 山东地方动态
 * @type {module.Bjp2p}
 */
module.exports = class YiCaiNews extends ArticleHrefAdapter {


    parseHtml(html, linkRule) {
        //将解析后的内容加载进解析器
        let $ = cheerio.load(html);
        let contents = [];
        let that = this;
        let rangeText = '';
        $(this.linkRule).each(function () {
          let liHtml = $(this).html();
          let $$ = cheerio.load(liHtml);
          let title = $$('h2').text().trim();
            if(!/\([0-9]+\)/g.test(title) && !/（[0-9]+）/g.test(title)) {
                let href = '';
                if ($(this).is('a') && $(this).attr('href')) {
                    href = urlTool.resolve(that.requestUrl, $(this).attr('href'));
                }

                if (title.length != 0 || href.length != 0) {
                    contents.push({
                        title: title,
                        href: href
                    });
                    rangeText += title + href;
                }
            }


        });

        return {
            rangeText: rangeText,
            contents: contents
        };
    }


}
